package com.sjd.mall.service.account.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.mall.domain.SjdAccountAddrDO;
import com.sjd.mall.domain.dto.account.AccountAddressAddRequestDTO;
import com.sjd.mall.domain.dto.account.AccountAddressUpdateRequestDTO;
import com.sjd.mall.mapper.SjdAccountAddrMapper;
import com.sjd.mall.service.account.AccountAddressService;
import com.sjd.wx.utils.ThirdSessionHolder;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: sjd
 * @Package: com.sjd.mall.service.account.impl
 * @ClassName: AccountAddressImpl
 * @Author: genglong
 * @Description:
 * @Date: 2022/1/12 16:26
 * @Version: 1.0
 */
@Service
@AllArgsConstructor
public class AccountAddressServiceImpl implements AccountAddressService {

    private final SjdAccountAddrMapper sjdAccountAddrMapper;

    @Override
    public AjaxResult insertAccountAddress(AccountAddressAddRequestDTO requestDTO) {
        SjdAccountAddrDO sjdAccountAddrDO = SjdAccountAddrDO.builder().accountId(ThirdSessionHolder.getThirdSession().getAccountId())
                .memberId(ThirdSessionHolder.getSjdMemberId()).username(requestDTO.getUsername()).mobile(requestDTO.getMobile())
                .province(requestDTO.getProvince()).provinceCode(requestDTO.getProvinceCode()).city(requestDTO.getCity())
                .cityCode(requestDTO.getCityCode()).area(requestDTO.getArea()).areaCode(requestDTO.getAreaCode()).address(requestDTO.getAddress())
                .createTime(new Date()).modifyTime(new Date()).build();
        sjdAccountAddrMapper.insertSelective(sjdAccountAddrDO);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateAccountAddress(AccountAddressUpdateRequestDTO requestDTO) {
        SjdAccountAddrDO sjdAccountAddrDO = sjdAccountAddrMapper.selectByPrimaryKey(requestDTO.getId());
        if (ObjectUtil.isEmpty(sjdAccountAddrDO)){
            return AjaxResult.error("邮寄地址不存在！");
        }
        sjdAccountAddrDO.setUsername(requestDTO.getUsername());
        sjdAccountAddrDO.setMobile(requestDTO.getMobile());
        sjdAccountAddrDO.setProvince(requestDTO.getProvince());
        sjdAccountAddrDO.setProvinceCode(requestDTO.getProvinceCode());
        sjdAccountAddrDO.setCity(requestDTO.getCity());
        sjdAccountAddrDO.setCityCode(requestDTO.getCityCode());
        sjdAccountAddrDO.setArea(requestDTO.getArea());
        sjdAccountAddrDO.setAreaCode(requestDTO.getAreaCode());
        sjdAccountAddrDO.setAddress(requestDTO.getAddress());
        sjdAccountAddrDO.setModifyTime(new Date());
        sjdAccountAddrMapper.updateByPrimaryKeySelective(sjdAccountAddrDO);
        return AjaxResult.success();

    }

    @Override
    public List<SjdAccountAddrDO> selectAccountAddress() {
        return sjdAccountAddrMapper.selectAccountAddress(ThirdSessionHolder.getThirdSession().getAccountId());
    }

    @Override
    public AjaxResult deleteAccountAddress(Long id) {
        SjdAccountAddrDO sjdAccountAddrDO = sjdAccountAddrMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isEmpty(sjdAccountAddrDO)){
            return AjaxResult.error("地址信息不存在！");
        }
        sjdAccountAddrMapper.deleteAccountAddress(id);
        return AjaxResult.success();
    }
}
